home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9619 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  32 lines

  1. Path: ix.netcom.com!chi-il4-12
  2. From: stefmit@ix.netcom.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Simple question about constructors
  5. Date: 3 Mar 1996 05:24:09 GMT
  6. Organization: Netcom
  7. Message-ID: <4hbadp$t0p@cloner4.netcom.com>
  8. References: <4ha7d2$quc@cloner4.netcom.com>
  9. NNTP-Posting-Host: chi-il4-12.ix.netcom.com
  10. X-NETCOM-Date: Sat Mar 02  9:24:11 PM PST 1996
  11. X-Newsreader: News Xpress Version 1.0 Beta #4
  12.  
  13. It seems that the "copy constructor" day is one of my worst in the C++ 
  14. learning process. Now I ran into another problem:
  15. I have a linked list based on a template <class My_type>, which has a node 
  16. class with (obviously) a pointer (let's call it *next) and an element field of 
  17. the type My_type (called data). Then, I have a list class using the node 
  18. above, with its own pointer *head (I am trying to build a sort of stack). I 
  19. was able to build the stack (this was pretty easy), but then I tried to define 
  20. a copy constructor in the list, using something similar to:
  21. template <class My_type>
  22. List<My_type> :: List(const List &newList)
  23. { head -> data = newList.head -> data;
  24.   head -> next = newList.head -> next; }
  25. I am almost positive that the second line crashes my program when I try to 
  26. intialize an object list based on an existing one (as I copy pointers, instead 
  27. of what they point to - i.e. next), but I have no clue on how to address the 
  28. problem. The example I have in my book deals with strings, thus the 
  29. availability of strcpy or strup functions, that handle the copy. Would anybody 
  30. care to enlighten me on this?
  31. TIA.
  32.